package io.github.lee.repository.jel.repo

import android.util.Log
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.github.lee.core.repository.BaseRepository
import io.github.lee.core.repository.data.Response
import io.github.lee.repository.jel.api.ApiModule
import io.github.lee.repository.jel.api.ItemApi
import io.github.lee.repository.jel.data.Page
import io.github.lee.repository.jel.domain.Item
import retrofit2.http.GET
import javax.inject.Inject

class ItemRepository @Inject constructor() : BaseRepository() {

    private val api: ItemApi
        get() = ApiModule.itemApi

    //===Desc:=============================================================
    suspend fun playingList(
        userId: String? = "",
        page: Int? = 1, size: Int? = 10,
        imageTypes: String? = null,
        searchTerm: String? = null,
        fields: List<String>? = null,
        mediaTypes: List<String>? = null,
    ): Response<Page<Item>> {
        val startIndex = ((page ?: 1) - 1) * (size ?: 10)
        return api.playingList(
            userId ?: "",
            startIndex, size,
            imageTypes,
            searchTerm,
            fields,
            mediaTypes
        )
    }

    suspend fun latest(
        userId: String? = "",
        parentId: String?,
        page: Int = 1,
        size: Int? = 10,
    ): Response<List<Item>> {
        val startIndex = (page - 1) * (size ?: 10)

        return api.latest(userId ?: "", parentId ?: "", startIndex, size)
    }


    suspend fun localPlaying(): Page<Item> {
        Log.e("TAG", "-09876")
        val json =
            "{\"Items\":[{\"Name\":\"\\u81EA\\u7531\\u804C\\u4E1A\\u8005\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"c418ac24f0c4cc8902c27244ae552633\",\"HasSubtitles\":true,\"Container\":\"mkv,webm\",\"PremiereDate\":\"2023-10-06T00:00:00.0000000Z\",\"OfficialRating\":\"R\",\"ChannelId\":null,\"CommunityRating\":5.7,\"RunTimeTicks\":65140588544,\"ProductionYear\":2023,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":73.30589683994859,\"PlaybackPositionTicks\":47751892639,\"PlayCount\":3,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-03-01T12:28:06.2709235Z\",\"Played\":false,\"Key\":\"897087\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"5d6242e1ac5e923104c60c74e13fb5ef\",\"Logo\":\"a78686d0bb716147a0548da0ff735632\"},\"BackdropImageTags\":[\"51094438cebc9a24701f5164bd24c880\"],\"ImageBlurHashes\":{\"Backdrop\":{\"51094438cebc9a24701f5164bd24c880\":\"WVAUN?#kZ\$%1bvOX=qs;V[xaW;RjskXAR6ofWVRjysaJMyocogR.\"},\"Primary\":{\"5d6242e1ac5e923104c60c74e13fb5ef\":\"dqG\\u002B%W-pRi%2~VxaM{xa%3o}Naa#.9x[kWf\\u002B,.sCoyxa\"},\"Logo\":{\"a78686d0bb716147a0548da0ff735632\":\"OoQJcbWBofn%jZWUjYxus:adkCjZo1WV4mRjt7aeoMfioL\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u975E\\u8BDA\\u52FF\\u62703\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"b568addf1d7b1a5a5bfb444e10e4f35b\",\"Container\":\"mkv,webm\",\"PremiereDate\":\"2023-12-30T00:00:00.0000000Z\",\"OfficialRating\":\"PG13\",\"ChannelId\":null,\"CommunityRating\":5.4,\"RunTimeTicks\":71643750400,\"ProductionYear\":2023,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":44.227378329708436,\"PlaybackPositionTicks\":31686152539,\"PlayCount\":2,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-02-29T12:47:44.7686474Z\",\"Played\":false,\"Key\":\"1211979\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"05b56a2419b69bffc1b2e7190f3e63ca\",\"Logo\":\"92e66fb5bf3ab3c3e1fe4a65fdc8b638\"},\"BackdropImageTags\":[\"4d4609afe054d2d5252d06de4f5200a2\"],\"ImageBlurHashes\":{\"Backdrop\":{\"4d4609afe054d2d5252d06de4f5200a2\":\"WkHndMtRNGjdNHxH~EkWbIR.jZxG^,bcoMa\$WCs:~CkDJ8R-WBof\"},\"Primary\":{\"05b56a2419b69bffc1b2e7190f3e63ca\":\"maIqNI?HOsT1}^xbS\$o4K,OFn\$bc0}NunhWAAIa#WBS3JXn*oMs:\"},\"Logo\":{\"92e66fb5bf3ab3c3e1fe4a65fdc8b638\":\"VJA\\u002B5V0yxakDRPwGFy,pWBOYAtwbo3ofNY#GxoEhwcS}\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"Following 1998\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"163add6c54f10f57467f39524b9d3866\",\"HasSubtitles\":true,\"Container\":\"mkv,webm\",\"ChannelId\":null,\"RunTimeTicks\":42102509568,\"ProductionYear\":1999,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":65.13839740528029,\"PlaybackPositionTicks\":27424900000,\"PlayCount\":3,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-02-16T02:53:01.6567934Z\",\"Played\":false,\"Key\":\"163add6c-54f1-0f57-467f-39524b9d3866\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{},\"BackdropImageTags\":[],\"ImageBlurHashes\":{},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u7231\\u5BA0\\u5927\\u673A\\u5BC62\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"4155b520231d9c292ff92df2ac5139a2\",\"HasSubtitles\":true,\"Container\":\"mkv,webm\",\"PremiereDate\":\"2019-07-05T00:00:00.0000000Z\",\"OfficialRating\":\"PG\",\"ChannelId\":null,\"CommunityRating\":6.8,\"RunTimeTicks\":51671601152,\"ProductionYear\":2019,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":50.239857719205204,\"PlaybackPositionTicks\":25959738900,\"PlayCount\":9,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-02-15T01:38:20.8122053Z\",\"Played\":false,\"Key\":\"412117\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"694690a4f72c4cf8c4822e0736e4d7a3\"},\"BackdropImageTags\":[\"ed46028a7a01af25f25f382915fb77ed\"],\"ImageBlurHashes\":{\"Backdrop\":{\"ed46028a7a01af25f25f382915fb77ed\":\"WdAUB.IBRO%IM}t8?wIBRP%MM{of-:MyV@x[RjoKx^M|aet6R-oL\"},\"Primary\":{\"694690a4f72c4cf8c4822e0736e4d7a3\":\"dgSF*4?b.8~q?boeWqozbIkCadV@-;ofazoftRofRjV@\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u9A71\\u9B54\\u4EBA\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"dc3897e8970788479acbaadf4c9f5bce\",\"HasSubtitles\":true,\"Container\":\"mkv,webm\",\"PremiereDate\":\"1973-12-26T00:00:00.0000000Z\",\"OfficialRating\":\"R\",\"ChannelId\":null,\"CommunityRating\":7.7,\"RunTimeTicks\":79403909120,\"ProductionYear\":1973,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":9.608191944895522,\"PlaybackPositionTicks\":7629280000,\"PlayCount\":3,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-02-14T06:58:09.4761569Z\",\"Played\":false,\"Key\":\"9552\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"110da35316f6f777cd5351597058d533\"},\"BackdropImageTags\":[\"32ea20702867e90c962046b31b717154\"],\"ImageBlurHashes\":{\"Backdrop\":{\"32ea20702867e90c962046b31b717154\":\"V98qD?xr4fV{?UN3ayxrWERU09Rn^]xpD:?WjaD;xq-%\"},\"Primary\":{\"110da35316f6f777cd5351597058d533\":\"dI9H98?aM|tQt6xtogWB01D*t6M{D*D*M{j[-:xuWCtQ\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u60CA\\u5947\\u961F\\u957F2\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"96bcc478091952c21183f3177c97c95d\",\"HasSubtitles\":true,\"Container\":\"mkv,webm\",\"PremiereDate\":\"2023-11-10T00:00:00.0000000Z\",\"OfficialRating\":\"PG-13\",\"ChannelId\":null,\"CommunityRating\":5.2,\"RunTimeTicks\":62781251584,\"ProductionYear\":2023,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":17.51355655165398,\"PlaybackPositionTicks\":10995230000,\"PlayCount\":3,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-02-12T13:24:04.5467969Z\",\"Played\":false,\"Key\":\"609681\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"31f237db9291d44bf3c7bedcc3eeba7f\"},\"BackdropImageTags\":[\"593cb033d7052a9ef9b24794aa93b43c\"],\"ImageBlurHashes\":{\"Backdrop\":{\"593cb033d7052a9ef9b24794aa93b43c\":\"WQB{c\$K70J#Qk5t2\$SShNts9s\\u002BWA55wb%LS\$xJNbX9njjDbaSjj]\"},\"Primary\":{\"31f237db9291d44bf3c7bedcc3eeba7f\":\"dKDIz.x]0fWu}tS2E\\u002BxbR3jXt2RPto%1VXE2E1aLS4Sx\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u4E09\\u5927\\u961F\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"078baf8fbef89b6b87ab5d37a5489e27\",\"Container\":\"mov,mp4,m4a,3gp,3g2,mj2\",\"ChannelId\":null,\"RunTimeTicks\":27774289920,\"IndexNumber\":1,\"ParentIndexNumber\":1,\"IsFolder\":false,\"Type\":\"Episode\",\"ParentBackdropItemId\":\"ffc00ea10d5d57a0a7d0241fd91b2172\",\"ParentBackdropImageTags\":[\"14966b7f243d9a8e75a1c7572615266b\"],\"UserData\":{\"PlayedPercentage\":78.30062508399135,\"PlaybackPositionTicks\":21747442620,\"PlayCount\":3,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-02-12T13:13:24.3218463Z\",\"Played\":false,\"Key\":\"tt29363617001001\"},\"SeriesName\":\"\\u4E09\\u5927\\u961F\",\"SeriesId\":\"ffc00ea10d5d57a0a7d0241fd91b2172\",\"SeasonId\":\"920326d5179cca03494b1fcf57d595e7\",\"PrimaryImageAspectRatio\":0.5625,\"SeriesPrimaryImageTag\":\"759604adf0ec030a2abf070905bc9681\",\"SeasonName\":\"\\u4E09\\u5927\\u961F\",\"VideoType\":\"VideoFile\",\"ImageTags\":{},\"BackdropImageTags\":[],\"ImageBlurHashes\":{\"Backdrop\":{\"14966b7f243d9a8e75a1c7572615266b\":\"WGB.om~Ar^IVMeR5~T={NKR6MyM{NHRkR\\u002BR-oekBQ.R6\$zb^x[%L\"},\"Primary\":{\"759604adf0ec030a2abf070905bc9681\":\"mcHS~6WBE1%1|ZWBozso\$PsokBR*wgsoSLWV#8w|bvjY^5sCV@xF\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u4E0A\\u6D77\\u6B63\\u5348\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"fe5cab1780968181fe92a22cbd90c018\",\"HasSubtitles\":true,\"Container\":\"mkv,webm\",\"PremiereDate\":\"2000-06-01T00:00:00.0000000Z\",\"OfficialRating\":\"PG-13\",\"ChannelId\":null,\"CommunityRating\":6.5,\"RunTimeTicks\":66286399488,\"ProductionYear\":2000,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":81.21617770134874,\"PlaybackPositionTicks\":53835280000,\"PlayCount\":4,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-02-04T00:00:06.7610785Z\",\"Played\":false,\"Key\":\"8584\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"bd42561eeb6572df38e01d9b1bb7d8df\"},\"BackdropImageTags\":[\"98965b26f145857c3ffecba85eb40f07\"],\"ImageBlurHashes\":{\"Backdrop\":{\"98965b26f145857c3ffecba85eb40f07\":\"WzG],gM{T}%he:yY?bNeR*n\$M|j[xvNfaejYM{WAyEfQt7xuV@oI\"},\"Primary\":{\"bd42561eeb6572df38e01d9b1bb7d8df\":\"drD^1PWCXntS?wR\\u002Bo~o#t8R.n~WAxtW?VsV@o#W=oIjs\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u6D89\\u8FC7\\u6124\\u6012\\u7684\\u6D77\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"7622900821fb2ca8580e85029300e416\",\"HasSubtitles\":true,\"Container\":\"mov,mp4,m4a,3gp,3g2,mj2\",\"PremiereDate\":\"2023-11-25T00:00:00.0000000Z\",\"ChannelId\":null,\"CommunityRating\":7.4,\"RunTimeTicks\":86354780160,\"ProductionYear\":2023,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":9.655599405789744,\"PlaybackPositionTicks\":8338071640,\"PlayCount\":1,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-01-21T09:59:15.2300662Z\",\"Played\":false,\"Key\":\"855492\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"d6a980a83f85c0f75264a27fc4c0bd16\"},\"BackdropImageTags\":[\"93b603adae1b163f9bf784540a1a7016\"],\"ImageBlurHashes\":{\"Backdrop\":{\"93b603adae1b163f9bf784540a1a7016\":\"W596XYJB5R-;-p}@}s#R0fM{xDIU5l^QS2I;-pEgS3OY-VS#XS%2\"},\"Primary\":{\"d6a980a83f85c0f75264a27fc4c0bd16\":\"m7A0B,=|EM%g{xIAIo\$%?vxuR%%ME1wIo}EgNes9IoN_kCXSJ7jZ\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"},{\"Name\":\"\\u575A\\u5982\\u78D0\\u77F3\",\"ServerId\":\"5af8e833a7d146e685acbfee61d6a1e1\",\"Id\":\"789e26577c9d983d274c89e04c52874a\",\"HasSubtitles\":true,\"Container\":\"mov,mp4,m4a,3gp,3g2,mj2\",\"PremiereDate\":\"2023-09-28T00:00:00.0000000Z\",\"OfficialRating\":\"15A\",\"ChannelId\":null,\"CommunityRating\":6.1,\"RunTimeTicks\":76520882176,\"ProductionYear\":2023,\"IsFolder\":false,\"Type\":\"Movie\",\"UserData\":{\"PlayedPercentage\":33.19272250623145,\"PlaybackPositionTicks\":25399364080,\"PlayCount\":3,\"IsFavorite\":false,\"LastPlayedDate\":\"2024-01-13T09:53:03.2274598Z\",\"Played\":false,\"Key\":\"661579\"},\"VideoType\":\"VideoFile\",\"ImageTags\":{\"Primary\":\"cecbce1ec3b0db208808b025338b6369\"},\"BackdropImageTags\":[\"f8bb68f2a95ccb90af5c082f7002735f\"],\"ImageBlurHashes\":{\"Backdrop\":{\"f8bb68f2a95ccb90af5c082f7002735f\":\"W65X=SkrNbt6I9D%~WbvRjsRE1E1-SV@adsmIpNHe,RPayt7N{bc\"},\"Primary\":{\"cecbce1ec3b0db208808b025338b6369\":\"dB7K;,00-pxuD*.8ITn%V@ofkBRjnhRjozogi^V@oza}\"}},\"LocationType\":\"FileSystem\",\"MediaType\":\"Video\"}],\"TotalRecordCount\":10,\"StartIndex\":0}"
        return Gson().fromJson(json, object : TypeToken<Page<Item>>() {}.type)
    }

    suspend fun itemDetail(userId: String?, itemId: String?): Response<Item> {
        return api.itemDetail(userId ?: "", itemId ?: "")
    }


    @GET("/Users/{userId}/Items")
    suspend fun userItems(
        userId: String?,
        parentId: String?,
        page: Int = 1,
        size: Int = 10,
        recursive: Boolean? = true,
        includeItemTypes: List<String>? = null,
        filters: List<String>? = null,
        sortBy: List<String>? = null,
        sortOrder: String? = null,
    ): Response<Page<Item>> {
        val startIndex = (page - 1) * size
        return api.userItems(
            userId ?: "",
            parentId ?: "",
            startIndex,
            size,
            recursive,
            includeItemTypes,
            filters,
            sortBy,
            sortOrder
        )
    }

}